package com.meiyu.live.ui.login;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response;
import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.net.param.ResetParams;
import com.meiyu.live.net.response.BaseResponse;
import com.meiyu.live.util.ToastUtil;
import com.meiyu.live.widget.NetLoadingDailog;

/**
 * Created by zbl on 2016/9/22.
 */
public class ResetPasswordInputActivity extends BaseActivity implements View.OnClickListener {

    protected NetLoadingDailog dialog_loading;
    private View btn_back;
    private TextView btn_reset;
    private EditText et_password, et_password_again;
    private String account;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resetpasswordinput);
        init();
        account = getIntent().getStringExtra("account");
    }

    private void init() {
        dialog_loading = new NetLoadingDailog(this);
        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
        btn_reset = (TextView) findViewById(R.id.btn_reset);
        btn_reset.setOnClickListener(this);
        btn_reset.setEnabled(false);
        et_password = (EditText) findViewById(R.id.et_password);
        et_password_again = (EditText) findViewById(R.id.et_password_again);
        et_password.addTextChangedListener(textWatcher);
        et_password_again.addTextChangedListener(textWatcher);
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (et_password.getText().length() > 0 && et_password_again.getText().length() > 0) {
//                btn_reset.setTextColor(0xff593512);
                btn_reset.setBackgroundResource(R.drawable.background_btn_startlive);
                btn_reset.setEnabled(true);
            } else {
//                btn_reset.setTextColor(0xffffffff);
                btn_reset.setBackgroundResource(R.drawable.background_login_disable);
                btn_reset.setEnabled(false);
            }
        }
    };

    @Override
    public void onClick(View view) {
        if (view == btn_back) {
            finish();
        } else if (view == btn_reset) {
            getData(et_password.getText().toString().trim(), et_password_again.getText().toString().trim());
        }
    }


    private void getData(final String password, final String password_again) {
        if (TextUtils.isEmpty(password)) {
            ToastUtil.show("请输入密码");
            return;
        } else if (TextUtils.isEmpty(password_again)) {
            ToastUtil.show("请再次输入密码");
            return;
        } else if (!password.equals(password_again)) {
            ToastUtil.show("两次密码输入不一致");
            return;
        }
        long accountNumber = 0;
        try {
            accountNumber = Long.parseLong(account);
        } catch (NumberFormatException e) {
            ToastUtil.show("账号错误");
            return;
        }
        dialog_loading.loading();
        request(new ResetParams(accountNumber,password), BaseResponse.class, new Response.Listener<BaseResponse>() {
            @Override
            public void onResponse(BaseResponse onlyStatusResponse) {
                dialog_loading.dismissDialog();
                if (onlyStatusResponse.getStatusCode() == Constant.StatusCode_Success){
                    ToastUtil.show("密码重置成功，请用新密码登录");
                    finish();
                }else{
                    ToastUtil.show("重置失败");
                }
            }
        });
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onError() {
        super.onError();
        dialog_loading.dismissDialog();
    }
}
